<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Lib\Params\InfoSearchParams;
use App\Lib\Params\WebColumnSearchParams;
use App\Model\Content;
use App\Model\Info;
use App\Model\WebColumn;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class TestController extends Controller {

    public function test()
    {
//        echo 'aaa';exit;
        return View('test');
    }

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//d

	}

    public function search()
    {
        $searchData = Input::get('searchData',array());

        $infoSearch = new \App\Lib\Params\InfoSearchParams($searchData);
        $infos = Info::search($infoSearch);

        $webColumnSearch = new WebColumnSearchParams(array('parentId'=>1));
        $firstWebColumns = WebColumn::search($webColumnSearch);
        $secondWebColumns = array();

        if($infoSearch->firstColumnId > 0){
            $webColumnSearch->setParentId($infoSearch->firstColumnId);
            $secondWebColumns = WebColumn::search($webColumnSearch);
        }
        return View('InfoList')
            ->with('infoSearch',$infoSearch)
            ->with('infos',$infos)
            ->with('firstWebColumns',$firstWebColumns)->with('secondWebColumns',$secondWebColumns);
    }

    public function info($infoId=0)
    {
        $info = Info::getInfoById($infoId);
        if(is_null($info)){
            //TODO
        }

        $webColumnSearch = new WebColumnSearchParams(array('parentId'=>1));
        $firstWebColumns = WebColumn::search($webColumnSearch);
        $secondWebColumns = array();
        $parentId = 0;
        if(isset($info->column_id) && $info->column_id > 0){
            $parentId = WebColumn::getParentId($info->column_id);
            if($parentId > 0){
                $webColumnSearch->setParentId($parentId);
                $secondWebColumns = WebColumn::search($webColumnSearch);
            }
        }

        return View('Info')
            ->with('info',$info)
            ->with('parentId',$parentId)
            ->with('firstWebColumns',$firstWebColumns)->with('secondWebColumns',$secondWebColumns);
    }

    public function content($infoId=0,$pageNo=1)
    {
        $info = Info::getInfoById($infoId,array('id','title'));
        if(is_null($info)){
            //TODO
        }

        $content = Content::getContentByInfoIdAndPageNo($infoId,$pageNo,array('id','page_no','content'));
//        if(is_null($content)){
//            $content = array('');
//        }

        return View('Content')
            ->with('info',$info)
            ->with('content',$content);
    }

    public function save()
    {
        $infoInput = Input::get('Info',null);
        if(is_null($infoInput)){

        }

        $info = Info::getInfoById($infoInput['infoId'],'id');
        if(is_null($info)){
            //TODO
        }
        $operatorId = 1;
        $nowTime = Date('Y-m-d H:i:s');
        $content = array('info_id'=>$infoInput['infoId'],'content'=>htmlentities($infoInput['content']),'page_no'=>$infoInput['page_no'],'operator_id'=>$operatorId,'update_time'=>$nowTime);

        $result = false;
        if(empty($infoInput['id'])){
            $content['create_time'] = $nowTime;
            $result = Content::addContent($content);
        }else{
            $result = Content::updateContentByInfoIdAndPageNo($infoInput['infoId'],$infoInput['page_no'],$content);
        }
        if(false === $result){
            echo '失败';
        } else{
            echo '成功';
        }
    }
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
